﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Newtonsoft.Json;
using DataTablesInteractions;

namespace SEP.Admin
{
    public partial class DataTableExample : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            /*
DataTables needs two things to work properly:
1 - Column headers
2 - Data

Both have to be encoded as JSON objects.
Both are actually bound to the grid from Javascript code.

So we do this:
1 - Run our query
2 - Grab the column headers from the result
3 - Grab the data from the result
4 - Encode #s 2 and 3 as JSON objects JsonConvert (an external library)
5 - Push them into hidden fields so that our javascript code can see them
*/

            //just a dumb query to try out
            string connstr = "SEP";
            string sqlqry = @"select FIRSTNAME AS ""First name"",LASTNAME AS ""Last name"" from constituent";
            DatabaseWrapper dbw = new DatabaseWrapper(connstr, sqlqry, System.Data.CommandType.Text);
            JsonDataManager jsonMgr = new JsonDataManager(dbw.ExecuteDataTable());

            //expose JSON data
            Page.ClientScript.RegisterHiddenField("jsonColumnHeaders", jsonMgr.ColumnHeadersAsJson());
            string jsondata = jsonMgr.DataAsJson();
            Page.ClientScript.RegisterHiddenField("jsonDataSource", jsondata);
        }
    }
}